import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../entity/user_entity.dart';
import '../page/index_page.dart';
import '../page/register_page.dart';
import '../util/dialog_util.dart';
import '../util/dio_util.dart';
import '../util/navigator_util.dart';
import '../util/shared_preferences_util.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _Page_Body(),
    );
  }
}

class _Page_Body extends StatefulWidget {
  @override
  __Page_BodyState createState() => __Page_BodyState();
}

class __Page_BodyState extends State<_Page_Body> {
  final _phoneCon = TextEditingController();
  final _passCon = TextEditingController();
  final _loginWidgetPadding = EdgeInsets.fromLTRB(0, 20, 0, 0);

  @override
  Widget build(BuildContext context) {
    return Center(
        child: Column(
      children: <Widget>[
        Container(
            width: MediaQuery.of(context).size.width,
            height: 227.0,
            color: Colors.blue,
            child: Image.asset(
              "image/login.jpg",
              fit: BoxFit.cover,
            )),
        Padding(
          padding: EdgeInsets.fromLTRB(0, 60, 0, 0),
          child: Container(
            width: 300.0,
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 20),
                  child: TextField(
                    controller: _phoneCon,
                    inputFormatters: [
                      WhitelistingTextInputFormatter.digitsOnly
                    ],
                    decoration: InputDecoration(
                      labelText: "请输入手机号",
                    ),
                  ),
                ),
                TextField(
                  controller: _passCon,
                  obscureText: true,
                  decoration: InputDecoration(
                    labelText: "请输入密码",
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    Container(
                      child: Center(
                        child: new GestureDetector(
                          child: Text(
                            "忘了密码？",
                            style: TextStyle(color: Colors.black26),
                          ),
                          onTap: () {
                            print("点击忘了密码");
                          },
                        ),
                      ),
                      height: 46,
                    )
                  ],
                ),
              ],
            ),
          ),
        ),
        ButtonTheme(
          child: RaisedButton(
            child: Text("登陆"),
            color: Colors.blue,
            textColor: Colors.white,
            onPressed: () {
              _sendLoginReq(_phoneCon.text, _passCon.text, context);
            },
          ),
          minWidth: 300.0,
          height: 46,
        ),
        Padding(
          padding: _loginWidgetPadding,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                "没有账号？",
                style: TextStyle(color: Colors.black26),
              ),
              new GestureDetector(
                child: Text(
                  "注册",
                  style: TextStyle(color: Colors.blue),
                ),
                onTap: () {
                  NavigatorUtil().push(context, RegisterPage(), true);
                },
              )
            ],
          ),
        ),
        GestureDetector(
          child: Text("管理员测试通道",style: TextStyle(fontSize: 15,color: Colors.black26),),
          onTap: (){
            _sendLoginReq("13170314323", "admin", context);
          },
        ),
        Expanded(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Text(
                "POWERED by 戴佳鹏",
                style: TextStyle(color: Colors.black12),
              )
            ],
          ),
          flex: 1,
        )
      ],
    ));
  }

  _sendLoginReq(String phone, String password, BuildContext context) async {
    var response = await DioUtil()
        .sendPostReq("user/login", {"phone": phone, "password": password});

    Map resData = response.data;

    UserEntity loginEntity = UserEntity().fromJson(resData);
    if (loginEntity.success) {
      await SpUtilForUser().pushUser(loginEntity);
      AlertDialogUtil().show(context, "登录", loginEntity.massage, onPressed: () {
        NavigatorUtil().push(context, IndexPage(), false);
      });
    } else {
      AlertDialogUtil().show(context, "登录", loginEntity.massage);
    }
  }
}
